//
//  JSAFNTools.m
//  IJSOCproject
//
//  Created by shange on 2017/4/26.
//  Copyright © 2017年 jinshan. All rights reserved.
//

#import "JSAFNTools.h"
#import <AFNetworking/AFNetworking.h>


@implementation JSAFNTools


+ (void)loadHTTPDataWithURL:(NSString *)url
                 parameters:(NSMutableDictionary *)parameters
                 methodType:(MethodType)type
               callBackData:(callBackData)blockData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/plain", @"application/x-javascript", @"application/javascript", nil]];

    NSString *urlStrCN = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    // 包含中文则转码
    NSString *rightURL = [urlStrCN stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // 包含乱码则转码
    if (type == 0) {
        [manager GET:rightURL parameters:parameters progress:nil success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
            blockData(responseObject, nil);
        } failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
            blockData(nil, error);
        }];
    } else if (type == 1) {
        [manager POST:rightURL parameters:parameters progress:nil success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
            blockData(responseObject, nil);
        } failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
            blockData(nil, error);
        }];
    } else {
        NSLog(@"请求方式不对");
    }
}


@end
